import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yi_shop/model/address.dart';
import 'package:yi_shop/route/index.dart';
import 'package:yi_shop/ui/scroll_refresh_config.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/svg.dart';
import 'package:yi_shop/utils/tools.dart';

class AddressReceiveController extends GetxController {
  RxList<Rx<AddressReceiveModel>> pageList = RxList([]);

  RefreshController refreshController = RefreshController();

  @override
  onInit() {
    pageList.add(Rx(AddressReceiveModel(
        name: '张三',
        mobile: '135*****032',
        district: '广西壮族自治区北海市铁山港区',
        address: '望京方恒国际中心B座',
        checked: true)));
    initData();
    super.onInit();
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 32) {
      return refreshController.loadNoData();
    }

    /// 添加数据
    initData();

    /// 完毕
    refreshController.loadComplete();
  }

  void initData() {
    for (var i = 0; i < 10; i++) {
      pageList.add(Rx(AddressReceiveModel(
          name: '张三', mobile: '135*****032', district: '广西壮族自治区北海市铁山港区', address: '望京方恒国际中心B座')));
    }
  }
}

class AddressReceivePage extends GetView<AddressReceiveController> {
  const AddressReceivePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '收货地址',
          style: StylesUtils.customTextStyle(
              fontWeight: FontWeight.w700, fontSize: StylesUtils.fontSize18),
        ),
        actions: [
          IconButton(
              onPressed: () {
                // Get.toNamed(RoutePath.addressManage);
                Get.toNamed(RoutePath.addressAdd);
              },
              icon: Icon(
                Icons.add,
                weight: 700,
                size: 30,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorFfff),
              ))
        ],
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  ScrollRefreshConfig scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  ListView listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (_, int index) {
        var row = controller.pageList[index];
        return Container(
          height: 110,
          margin: EdgeInsets.only(top: 15.w),
          padding: EdgeInsets.only(top: 12.h, bottom: 12.h),
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            children: [
              Container(
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.only(left: 30.w),
                child: Text(
                  '${row.value.name}    ${row.value.mobile}',
                  style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 10.h),
                padding: EdgeInsets.symmetric(horizontal: 10.w),
                alignment: Alignment.centerLeft,
                child: Row(
                  children: [
                    Expanded(
                        child: Row(
                      children: [
                        Wrap(
                          spacing: 5.w,
                          crossAxisAlignment: WrapCrossAlignment.center,
                          alignment: WrapAlignment.start,
                          children: addChildren(row),
                        ),
                        Expanded(
                            child: Container(
                          alignment: Alignment.centerLeft,
                          padding: const EdgeInsets.only(left: 5),
                          child: Text(
                            row.value.address,
                            style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ))
                      ],
                    )),
                    IconButton(
                        onPressed: () {
                          Get.toNamed(RoutePath.addressEdit, arguments: {'row': row.value});
                        },
                        icon: SvgUtils.assetName('u11277', width: 16, height: 16)),
                  ],
                ),
              )
            ],
          ),
        );
      },
      itemCount: controller.pageList.length,
    );
  }

  List<Widget> addChildren(Rx<AddressReceiveModel> row) {
    List<Widget> list = [];
    if (row.value.checked) {
      list.add(SvgUtils.assetName('u12520', width: 20, height: 20));
      list.add(Container(
        constraints: const BoxConstraints.expand(width: 40, height: 25),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            border: Border.all(color: ColorsUtils.parseColorFromHexString(ColorsUtils.color7979)),
            borderRadius: BorderRadius.circular(5)),
        child: Text(
          '默认',
          style: StylesUtils.customTextStyle(
              color: ColorsUtils.color3333, fontSize: StylesUtils.fontSize13),
        ),
      ));
    }
    list.add(SvgUtils.assetName('u16965', width: 20, height: 16));
    return list;
  }
}
